package flc.ast.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyMovieListBean;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class ContentAdapter extends StkProviderMultiAdapter<StkResBean> {

    public ContentAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new ContentProvider());
    }

    private List<StkResBean> listSp = new ArrayList<>();
    private boolean isEdit;
    private boolean isShowZhui;

    public void setListSp(List<StkResBean> listSp) {
        this.listSp = listSp;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void setShowZhui(boolean showZhui) {
        isShowZhui = showZhui;
    }

    private class ContentProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_content;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            Glide.with(getContext()).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivContentItemImg));
            baseViewHolder.setText(R.id.tvContentItemName, bean.getName());

            String str = new Gson().toJson(bean.getExtra());
            String director = getContext().getString(R.string.director);
            String actor = getContext().getString(R.string.actor);

            try {
                JSONObject extra = new JSONObject(str);
                JSONArray actorList = extra.optJSONArray("actorList");
                if (actorList != null) {
                    for (int i = 0; i < actorList.length(); i++) {
                        JSONObject item = actorList.getJSONObject(i);
                        if (item.getString("role").equals("导演")) {
                            director += item.getString("name") + " / ";
                        } else {
                            actor += item.getString("name") + " / ";
                        }
                    }
                }
                baseViewHolder.setText(R.id.tvContentItemDirector, director);
                baseViewHolder.setText(R.id.tvContentItemActor, actor);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            //评分
            double score = (double) bean.getScore_total() / (bean.getScore_count() == 0 ? 1 : bean.getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvContentItemScore, formattedNumber + getContext().getString(R.string.score));

            if (isEdit) {
                baseViewHolder.getView(R.id.ivContentItemDelete).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.ivContentItemDelete).setVisibility(View.GONE);
            }

            if (isShowZhui) {
                baseViewHolder.getView(R.id.ivContentItemZhui).setVisibility(View.VISIBLE);
                bean.setSelected(false);
                if (listSp != null && listSp.size() > 0) {
                    for (StkResBean stkResBean : listSp) {
                        if (stkResBean.getUrl().equals(bean.getUrl())) {
                            bean.setSelected(true);
                            break;
                        }
                    }
                }
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivContentItemZhui, R.drawable.ayizhuih);
                } else {
                    baseViewHolder.setImageResource(R.id.ivContentItemZhui, R.drawable.azhuiju);
                }
            } else {
                baseViewHolder.getView(R.id.ivContentItemZhui).setVisibility(View.GONE);
            }
        }
    }

}
